#!/usr/bin/perl -w

# TODO: Handle undefined $1
# TODO: Lock files

use MIME::Base64;
use LWP::UserAgent;
use XML::XPath;
use XML::XPath::XMLParser;

use FCTranscode;

$md_num = $#ARGV;
for (my $count = 0; $count <= $md_num; $count++){

	$ARGV[$count] =~ /^([w+\ ]+):(.+)/;
	$metadata{$1} = $2;
}
close $fh;

$ua = LWP::UserAgent->new;

#$ua->agent("add_item.pl/0.1");

$req = HTTP::Request->new(POST => "http://$host$path");
$req->protocol('HTTP/1.1');

$req->content_type('text/xml');

local $/=undef;

#open (FH, "<", "/Users/ecstv/fctranscode/blank_item.xml") or die("Couldn't open file: $!");


#$http_content = <FH>;
#close FH;


my $file = "/Users/ecstv/fctranscode/blank_item.xml";
my $xp = XML::XPath->new(filename => $file);

if ( ($metadata{'Title'} ne "") 	and
     ($metadata{'Title'} ne "[Title]")	)
		{ $xp->setNodeText("/eprint/title",$metadata{'Title'}); }

$tobep = $xp->find('/eprint/title');
print XML::XPath::XMLParser::as_string($tobep);


#Make the changes to $eprint

#print Dumper($eprint);

#$xs->XMLout($eprint,
#	KeepRoot => 1,
#	OutputFile => $file.".out",
#	XMLDecl => "<?xml version='1.0' ?>",
#	);

exit;

$req->content($http_content);

$req->header('Authorization', 'Basic '.encode_base64("$user:$pass"));

$req->header('Content-Length', length($http_content));

$req->header('X-Verbose', 'true');
$req->header('X-Packaging','http://eprints.org/ep2/data/2.0');

$res = $ua->request($req);

if ($res->is_success) {
	print $res->content;
}
else { print $res->status_line."\n".$res->content; }

